package co.yixiang.yshop.module.product.service.campaigndetail;

import co.yixiang.yshop.module.product.dal.dataobject.campaigndetail.CampaignDetailDO;
import co.yixiang.yshop.module.product.dal.mysql.campaigndetail.CampaignDetailMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;


/**
 * 营销活动明细 Service 实现类
 *
 * @author moxiangrong
 */
@Service
@Validated
public class CampaignDetailServiceImpl implements CampaignDetailService {

    @Resource
    private CampaignDetailMapper campaignDetailMapper;

    @Override
    public List<CampaignDetailDO> getCampaignDetailListByProductIds(List<Long> ids) {
        return campaignDetailMapper.selectList(new LambdaQueryWrapper<CampaignDetailDO>()
                .in(CampaignDetailDO::getProductId, ids));
    }

    @Override
    public List<CampaignDetailDO> getCampaignDetailListByCampaignId(Long id) {
        return campaignDetailMapper.selectList(new LambdaQueryWrapper<CampaignDetailDO>()
                .in(CampaignDetailDO::getCampaignId, id));
    }

    @Override
    public int campaignStockRemain(Long campaignDetailId) {
        CampaignDetailDO campaignDetailDO = findById(campaignDetailId);
        Long campaignId = campaignDetailDO.getCampaignId();
        List<CampaignDetailDO> list = campaignDetailMapper.selectList(new LambdaQueryWrapper<CampaignDetailDO>().
                eq(CampaignDetailDO::getCampaignId, campaignId)
        );
        return list.stream().mapToInt(CampaignDetailDO::getStock).sum();
    }

    @Override
    public Long getCampaignId(Long campaignDetailId) {
        return findById(campaignDetailId).getCampaignId();
    }

    private CampaignDetailDO findById(Long id) {
        return campaignDetailMapper.selectById(id);
    }
}
